/*
 * @Author: 陈永浩(cyh1231wp@163.com)
 * @Date: 2020-12-03 14:35:29
 * @LastEditors: 陈永浩(cyh1231wp@163.com)
 * @LastEditTime: 2021-02-23 20:53:10
 */
import store from '@/store'
import { constantRoutes } from '../../router'
import { getRouterList } from '../../api/system/public'
import { convertRouter } from '../../utils/router'

const state = () => ({
  routes: [],
  accessRoutes: null,
  partialRoutes: [],
})
const getters = {
  routes: (state) => state.routes,
  accessRoutes: (state) => state.accessRoutes,
}
const mutations = {
  setAllRoutes(state, routes) {
    state.accessRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
}
const actions = {
  async setAllRoutes({ commit }, payload) {
    let accessRoutes = []
    let { data } = await getRouterList()
    console.log(data);
    await store.dispatch('settings/changeNavBar', data)

    if (payload && payload.appId) {
      data = [{
        name: 'index',
        path: '/',
        url: 'layouts/index',
        children: data
      }]
    } else {
      data = [{
        name: 'settings',
        path: '/settings',
        url: 'layouts/settings',
        children: data
      }]
    }
    accessRoutes = convertRouter(data)

    commit('setAllRoutes', accessRoutes)
    return accessRoutes
  },
}
export default { state, getters, mutations, actions }
